﻿// CZoomableLabel.cpp
#include "CZoomableLabel.h"
#include <QPainter>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QDebug>
#include "Global.h"
CZoomableLabel::CZoomableLabel(QWidget *parent)
	: QLabel(parent)
	, m_scaleFactor(1.0)
	, m_isDragging(false)
{
	setMouseTracking(true);
	setFocusPolicy(Qt::StrongFocus);
	setAlignment(Qt::AlignCenter);
	setMinimumSize(200, 200);
	setStyleSheet("CZoomableLabel { border: 2px solid red; background-color: #f8f8f8; }");
	setCursor(Qt::OpenHandCursor);
}

void CZoomableLabel::setPixmap(const QPixmap &pixmap)
{
	qDebug() << A2T("CZoomableLabel::setPixmap - 接收到图片，尺寸:") << pixmap.size();
	m_originalPixmap = pixmap;
	m_scaleFactor = 1.0;
	m_dragOffset = QPoint(0, 0);
	m_isDragging = false;
	updateDisplay();
}

void CZoomableLabel::wheelEvent(QWheelEvent *event)
{
	qDebug() << A2T("=== 滚轮事件触发 ===");

	if (m_originalPixmap.isNull()) {
		QLabel::wheelEvent(event);
		return;
	}

	// 获取鼠标在图片上的相对位置（0-1之间的比例）
	QPointF mousePos;
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
	mousePos = event->position();
#else
	mousePos = event->posF();
#endif

	// 计算鼠标在原始图片上的位置比例
	double mouseXRatio = (mousePos.x() - m_dragOffset.x()) / (m_originalPixmap.width() * m_scaleFactor);
	double mouseYRatio = (mousePos.y() - m_dragOffset.y()) / (m_originalPixmap.height() * m_scaleFactor);

	// 保存缩放前的参数
	double oldScale = m_scaleFactor;
	QPoint oldOffset = m_dragOffset;

	// 计算缩放
	double zoomStep = 0.2;
	if (event->delta() > 0 || event->angleDelta().y() > 0) {
		// 放大
		m_scaleFactor *= (1.0 + zoomStep);
		qDebug() << A2T("放大，新缩放因子:") << m_scaleFactor;
	}
	else {
		// 缩小
		m_scaleFactor /= (1.0 + zoomStep);
		qDebug() << A2T("缩小，新缩放因子:") << m_scaleFactor;
	}

	// 计算新的偏移量，保持鼠标位置不变
	double scaleRatio = m_scaleFactor / oldScale;
	m_dragOffset.setX(mousePos.x() - (mouseXRatio * m_originalPixmap.width() * m_scaleFactor));
	m_dragOffset.setY(mousePos.y() - (mouseYRatio * m_originalPixmap.height() * m_scaleFactor));

	qDebug() << A2T("鼠标位置:") << mousePos << A2T("缩放比例:") << scaleRatio << A2T("新偏移:") << m_dragOffset;

	updateDisplay();
	event->accept();
}

void CZoomableLabel::mousePressEvent(QMouseEvent *event)
{
	qDebug() << A2T("=== 鼠标按下事件 ===");

	if (event->button() == Qt::LeftButton) {
		m_isDragging = true;
		m_lastMousePos = event->pos();
		setCursor(Qt::ClosedHandCursor);
		qDebug() << "开始拖拽，当前位置:" << m_lastMousePos;
		event->accept();
	}
	else {
		QLabel::mousePressEvent(event);
	}
}

void CZoomableLabel::mouseMoveEvent(QMouseEvent *event)
{
	if (m_isDragging) {
		QPoint delta = event->pos() - m_lastMousePos;
		m_dragOffset += delta;
		m_lastMousePos = event->pos();

		qDebug() << A2T("拖拽中，偏移量:") << m_dragOffset;
		updateDisplay();
		event->accept();
	}
	else {
		QLabel::mouseMoveEvent(event);
	}
}

void CZoomableLabel::mouseReleaseEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton && m_isDragging) {
		m_isDragging = false;
		setCursor(Qt::OpenHandCursor);
		qDebug() << A2T("结束拖拽");
		event->accept();
	}
	else {
		QLabel::mouseReleaseEvent(event);
	}
}

void CZoomableLabel::updateDisplay()
{
	if (m_originalPixmap.isNull()) {
		QLabel::setPixmap(QPixmap());
		setText(A2T("等待图片加载..."));
		return;
	}

	// 计算缩放后的图片
	QSize scaledSize = m_originalPixmap.size() * m_scaleFactor;
	QPixmap scaledPixmap = m_originalPixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

	// 创建与控件相同大小的画布
	QPixmap canvas(size());
	canvas.fill(Qt::transparent);

	QPainter painter(&canvas);

	// 计算绘制位置（居中 + 拖拽偏移）
	QPoint drawPos;
	drawPos.setX((width() - scaledPixmap.width()) / 2 + m_dragOffset.x());
	drawPos.setY((height() - scaledPixmap.height()) / 2 + m_dragOffset.y());

	// 绘制缩放后的图片
	painter.drawPixmap(drawPos, scaledPixmap);

	// 设置到QLabel
	QLabel::setPixmap(canvas);

	qDebug() << A2T("更新显示 - 控件大小:") << size()
		<< A2T("图片大小:") << scaledSize
		<< A2T("绘制位置:") << drawPos;
}